package cm.com.blackboard.DataModule4B2.customizeConfigUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 

public class DepartmentIdMapPropertiesUtil { 
   
	private static final Logger logger = LoggerFactory.getLogger(DepartmentIdMapPropertiesUtil.class);
	
    static String profilepath=DepartmentIdMapPropertiesUtil.class.getClassLoader().getResource("/departmentIdMap.properties").getPath();
	//static String profilepath = System.getProperty("user.dir")+"/target/classes/creditMap.properties";
	private static LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
    private static OrderedProperties props = new OrderedProperties(); 
    static { 
        try {
            props.load(new FileInputStream(profilepath)); 
            generateMap();
        } catch (FileNotFoundException e) {
        	logger.info("please make sure the creditMap file in correct filepath");
            e.printStackTrace(); 
        } catch (IOException e) { 
        	logger.info("please make sure the creditMap file in correct filepath");
        } 
    } 

    public static String getKeyValue(String key) { 
        return props.getProperty(key); 
    }
    
    public static String getKeyByValue(String comparedValue) { 
    	Iterator<Entry<Object, Object>> it = props.entrySet().iterator();  
        while (it.hasNext()) {  
            Entry<Object, Object> entry = it.next();
            Object key = entry.getKey();  
            Object value = entry.getValue();
            if(value.toString().equalsIgnoreCase(comparedValue))
            	return key.toString();
        }
        return ""; 
    } 

    public static void updateProperties(String keyname,String keyvalue) { 
        try { 
            props.load(new FileInputStream(profilepath)); 
            OutputStream fos = new FileOutputStream(profilepath);            
            props.setProperty(keyname, keyvalue);  
            props.store(fos, "Update '" + keyname + "' value"); 
        } catch (IOException e) { 
        	logger.info("properties file update error"); 
        } 
    }  
    
    public static Properties getProps(){	
    	return props;
    }
 
    public static void generateMap(){
    	Set<Object> keyset = props.keySet();  
        for (Object object : keyset) {  
            String propValue = props.getProperty(object.toString()).toString();  
            map.put(object.toString(), propValue.toString());  
        } 
    	List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());  
        //然后通过比较器来实现排序  
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {  
            //降序排序  
            public int compare(Entry<String, String> o1, Entry<String, String> o2) {  
                return o2.getKey().compareTo(o1.getKey());  
            }  
              
        });   
        map = new LinkedHashMap<String,String>();
        for (Map.Entry<String, String> mapping : list) {  
        	map.put(mapping.getKey(), mapping.getValue());  
        }  
    }
    
    public static LinkedHashMap<String,String> getMap(){
    	generateMap();
    	return map;
    }
    
    public static void main(String[] args) { 
    } 
} 

